from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import pandas as pd
import umap
import joblib
if __name__ == '__main__':
    # data = pd.read_excel("./data.xlsx")  # type: pd.DataFrame
    # data = data.drop(columns=['Unnamed: 4', 'Unnamed: 10', 'Unnamed: 22'])
    # print(data.columns.values)
    # data = data.replace('-', np.nan)
    # for i in data.columns[2:]:
    #     mean = data[i][4:].dropna()
    #     print(mean.shape)
    #     meanV = mean.mean()
    #     print(i, " mean ", meanV)
    #     data[i] = data[i].replace(np.nan, meanV)
    # v = data.values
    # v = v[4:, 2:]
    # print(v)
    # print(v.shape)
    data = pd.read_excel("./completion/completed.xlsx")  # type: pd.DataFrame
    data = data.drop('Unnamed: 0',1)
    v = data.values
    print(v.shape)
    # ssModel = StandardScaler().fit(v) # type: StandardScaler
    ssModel = joblib.load('standardModel.m') # type: StandardScaler
    # joblib.dump(ssModel,"standardModel.m")
    scaled_v = ssModel.transform(v)
    print(scaled_v)
    dataFrame = pd.DataFrame(scaled_v, columns=data.columns)
    with pd.ExcelWriter('scaled_v2.xlsx') as writer:  # 一个excel写入多页数据
        dataFrame.to_excel(writer, sheet_name='page1', float_format='%.6f')
    reducer = umap.UMAP(random_state=128)
    model = reducer.fit(scaled_v)
    joblib.dump(model,'model2.m')
    # model = joblib.load('model.m') # type: UMAP
    embedding = model.transform(scaled_v)
    # embedding = reducer.fit_transform(scaled_v)
    print(embedding.shape)
    print(embedding)

    plt.scatter(
        embedding[:, 0],
        embedding[:, 1],
    )
    plt.gca().set_aspect('equal', 'datalim')
    plt.title('UMAP projection of the Country dataset', fontsize=24)
    plt.savefig("location7.png")

    plt.show()
    dataFrame = pd.DataFrame(embedding, columns=['x', 'y'])
    with pd.ExcelWriter('location7.xlsx') as writer:  # 一个excel写入多页数据
        dataFrame.to_excel(writer, sheet_name='page1', float_format='%.6f')
